﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace WpfApplication1
{
    /// <summary>
    /// Months.xaml 的交互逻辑
    /// </summary>
    //月份控件
    public partial class Months : UserControl
    {
        Days[] day = new Days[31];
        public static DateTime now = DateTime.Now;
        int m = DateTime.Now.Month;
        private int showDays = 31;
        Storyboard OnNext = new Storyboard();
        Storyboard OnPre = new Storyboard();
        Storyboard OnReturn1 = new Storyboard();
        Storyboard OnReturn2 = new Storyboard();

        public Months()
        {
            InitializeComponent();

            #region 初始化动画
            this.Name = "month";
            NameScope.SetNameScope(this, new NameScope());
            this.RegisterName(this.Name, this);
            this.HorizontalAlignment = HorizontalAlignment.Left;
            this.VerticalAlignment = VerticalAlignment.Top;

            ThicknessAnimation onShow1 = new ThicknessAnimation(new Thickness(0, 0, 0, 0), new Thickness(500, 0, 0, 0), new Duration(TimeSpan.FromMilliseconds(400)));
            Storyboard.SetTargetName(onShow1, this.Name);
            Storyboard.SetTargetProperty(onShow1, new PropertyPath(Months.MarginProperty));

            ThicknessAnimation onShow2 = new ThicknessAnimation(new Thickness(0, 0, 0, 0), new Thickness(-500, 0, 0, 0), new Duration(TimeSpan.FromMilliseconds(400)));
            Storyboard.SetTargetName(onShow2, this.Name);
            Storyboard.SetTargetProperty(onShow2, new PropertyPath(Months.MarginProperty));

            ThicknessAnimation onShow3 = new ThicknessAnimation(new Thickness(500, 0, 0, 0), new Thickness(0, 0, 0, 0), new Duration(TimeSpan.FromMilliseconds(400)));
            Storyboard.SetTargetName(onShow3, this.Name);
            Storyboard.SetTargetProperty(onShow3, new PropertyPath(Months.MarginProperty));

            ThicknessAnimation onShow4 = new ThicknessAnimation(new Thickness(-500, 0, 0, 0), new Thickness(0, 0, 0, 0), new Duration(TimeSpan.FromMilliseconds(400)));
            Storyboard.SetTargetName(onShow4, this.Name);
            Storyboard.SetTargetProperty(onShow4, new PropertyPath(Months.MarginProperty));

            OnNext.Children.Add(onShow1);
            OnPre.Children.Add(onShow2);
            OnReturn1.Children.Add(onShow3);
            OnReturn2.Children.Add(onShow4);

            OnNext.Completed += delegate(object sender, EventArgs e)
            {
                m = m % 12 + 1;
                now=now.AddMonths(1);
                switch (m)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        showDays = 31;
                        break;
                    case 2:
                        showDays = 28;
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        showDays = 30;
                        break;
                }
                label1.Content = now.Year.ToString()+"年"+m.ToString() + "月";
                grid_days.Children.RemoveRange(1, grid_days.Children.Count);
                OnReturn1.Begin(this);
            };

            OnPre.Completed += delegate(object sender, EventArgs e)
            {
                m = m - 1 == 0 ? 12 : m - 1;
                now=now.AddMonths(-1);
                switch (m)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        showDays = 31;
                        break;
                    case 2:
                        showDays = 28;
                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        showDays = 30;
                        break;
                }
                label1.Content =now.Year.ToString()+"年"+ m.ToString() + "月";
                grid_days.Children.RemoveRange(1, grid_days.Children.Count);
                OnReturn2.Begin(this);
            };
            OnReturn1.Completed += delegate(object sender, EventArgs e)
            {
                UpdateDays();
            };
            OnReturn2.Completed += delegate(object sender, EventArgs e)
            {
                UpdateDays();
            };
            #endregion

            //加入日期控件
            for (int i = 0; i < 31; i++)
            {
                day[i] = new Days(i + 1, new Point(i % 7 * 42, i / 7 * 50 + 50), "d" + i.ToString());
                grid_days.Children.Add(day[i]);
            }
            label1.Content = now.Year.ToString() + "年" + m.ToString() + "月";
            
        }

        public void NextMonth()
        {
            OnNext.Begin(this);
        }

        public void PreMonth()
        {
            OnPre.Begin(this);         
        }
        private void UpdateDays()
        {
            for (int i = 0; i < showDays; i++)
            {
                day[i] = new Days(i + 1, new Point(i % 7 * 42, i / 7 * 50 + 50), "d" + i.ToString());
                grid_days.Children.Add(day[i]);
            }
        }

        private void UserControl_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta < 0)
            {
                NextMonth();
            }
            else if (e.Delta > 0)
            {
                PreMonth();
            }
        }
    }
}
